package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.NoticeTimeSelectAdapter;
import com.hxkcloud.patient.wheelview.WheelView;
import com.hxkcloud.patient.wheelview.adapter.NumericWheelAdapter;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by william_sun on 2016-07-12.
 * Description:新建提醒单，提醒时间选择
 */
public class NoticeTimeSelectActivity extends BaseActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_complete;

    private WheelView wheel_hour;
    private WheelView wheel_min;
    private LinearLayout ll_add;
    private RecyclerView rv;

    private int position;
    private ArrayList<String> times;
    private NoticeTimeSelectAdapter adapter;

    private String type = "";

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticetimeselect);

        times = this.getIntent().getStringArrayListExtra("times");
        position = this.getIntent().getIntExtra("position", 0);
        type = getIntent().getStringExtra("type");
        initView();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_complete = (TextView) findViewById(R.id.tv_title_func);

        wheel_hour = (WheelView) findViewById(R.id.wheel_noticetimeselect_hour);
        wheel_min = (WheelView) findViewById(R.id.wheel_noticetimeselect_min);
        ll_add = (LinearLayout) findViewById(R.id.ll_noticetimeselect_add);
        rv = (RecyclerView) findViewById(R.id.rv_noticetimeselect);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        if ("0".equals(type)) {
            tv_title.setText(getResources().getText(R.string.assistant_text_perform));
        } else if ("1".equals(type)) {
            tv_title.setText(getResources().getText(R.string.assistant_text_remind));
        } else if ("2".equals(type)) {
            tv_title.setText(getResources().getText(R.string.betterplan));
        }

        tv_complete.setVisibility(View.VISIBLE);
        tv_complete.setText(getResources().getText(R.string.save));
        iv_back.setOnClickListener(this);
        tv_complete.setOnClickListener(this);

        ll_add.setOnClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        Calendar c = Calendar.getInstance();
        int curHour = c.get(Calendar.HOUR_OF_DAY);
        int curMin = c.get(Calendar.MINUTE);

        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(this, 0, 23, "%02d");
        wheel_hour.setViewAdapter(numericWheelAdapter3);
        wheel_hour.setCyclic(false);

        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(this, 0, 59, "%02d");
        wheel_min.setViewAdapter(numericWheelAdapter4);
        wheel_min.setCyclic(false);

        wheel_hour.setVisibleItems(5);
        wheel_min.setVisibleItems(5);

        wheel_hour.setCurrentItem(curHour);
        wheel_min.setCurrentItem(curMin);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);
        adapter = new NoticeTimeSelectAdapter(mContext, times, new NoticeTimeSelectAdapter.NoticeTimeListener() {
            @Override
            public void onDelete(int position) {
                times.remove(position);
                adapter.setData(times);
            }
        });
        rv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                Intent iBack = new Intent(this, NewDolistActivity.class);
                iBack.putExtra("times", times);
                iBack.putExtra("position", position);
                setResult(RESULT_OK, iBack);
                finish();
                break;
            case R.id.ll_noticetimeselect_add:
                if (times.size() >= 8) {
                    Toast.makeText(mContext, R.string.most8timepoint, Toast.LENGTH_LONG).show();
                } else {
                    String hour = wheel_hour.getCurrentItem() < 10 ? "0" + wheel_hour.getCurrentItem() : wheel_hour.getCurrentItem() + "";
                    String min = wheel_min.getCurrentItem() < 10 ? "0" + wheel_min.getCurrentItem() : wheel_min.getCurrentItem() + "";
                    if (times.contains(hour + ":" + min)) {
                        toastMsg(mContext.getResources().getString(R.string.time_exists));
                        return;
                    }
                    times.add(hour + ":" + min);
                    adapter.setData(times);
                }
                break;
        }
    }
}
